package com.jing.shop.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jing.common.core.domain.R;
import com.jing.common.core.utils.FastUtil;
import com.jing.shop.domain.dto.ProductTypeDTO;
import com.jing.shop.domain.po.ProductTypePO;
import com.jing.shop.domain.vo.ProductShopTypeVO;
import com.jing.shop.domain.vo.ProductTypeVO;
import com.jing.shop.mapper.ProductTypeMapper;
import com.jing.shop.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private FastUtil fastUtil;

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public PageInfo<ProductTypeVO> findTypeList(ProductTypeDTO productTypeDTO) {
        PageHelper.startPage(productTypeDTO.getPageNum(),productTypeDTO.getPageSize());
        List<ProductTypeVO> typeList;
        if(productTypeDTO.getTypeLevel()==null&&
                (productTypeDTO.getTypeParentId()==null||productTypeDTO.getTypeParentId()<0)){
             typeList= productTypeMapper.findTypeList(productTypeDTO);
        }else{
             typeList = productTypeMapper.findType(productTypeDTO);
        }
        PageInfo<ProductTypeVO> productTypePOPageInfo = new PageInfo<>(typeList);
        return productTypePOPageInfo;
    }

    @Override
    public void addType(ProductTypeDTO productTypeDTO) {
        if(productTypeDTO.getTypeParentId()==0){
            productTypeDTO.setTypeLevel(1);
        }else{
            productTypeDTO.setTypeLevel(2);
        }
        Integer flag = productTypeMapper.addType(productTypeDTO);
        if(flag<=0){
            throw new RuntimeException("添加异常");
        }
    }

    @Override
    public void updateType(ProductTypeDTO productTypeDTO) {
        Integer flag = productTypeMapper.updateType(productTypeDTO);
        if(flag<=0){
            throw new RuntimeException("修改异常");
        }
    }

    @Override
    public void delTypeId(Integer typeId) {
        Integer flag = productTypeMapper.delTypeId(typeId);
        if(flag<=0){
            throw new RuntimeException("删除异常");
        }
    }

    @Override
    public List<ProductTypeVO> findList() {
        List<ProductTypeVO> list = productTypeMapper.findList();
        return list;
    }

    @Override
    public void updatetypeNavigation(ProductTypeDTO productTypeDTO) {
        Integer flag = productTypeMapper.updatetypeNavigation(productTypeDTO);
        if(flag<=0){
            throw new RuntimeException("修改导航栏状态失败");
        }
    }

    @Override
    public void updatetypeDisplay(ProductTypeDTO productTypeDTO) {
        Integer flag = productTypeMapper.updatetypeDisplay(productTypeDTO);
        if(flag<=0){
            throw new RuntimeException("修改显示状态失败");
        }
    }

    @Override
    public R upload(MultipartFile multipartFile) {
        String url="";
        try {
            url = fastUtil.upload(multipartFile);
            if(url.equals("")){
                return R.fail("上传图片失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok("http://106.54.45.36:8888/"+url);
    }

    @Override
    public ArrayList<ProductShopTypeVO> diGui(Integer typeLevel) {
        ArrayList<ProductShopTypeVO> productTypeDTOS = productTypeMapper.dixGui();
        ArrayList<ProductShopTypeVO> chidRen = getChidRen(productTypeDTOS, typeLevel);
        return chidRen;
    }

    @Override
    public ArrayList<ProductShopTypeVO> getChidRen(List<ProductShopTypeVO> productTypeDTOS, Integer typeLevel) {
        //建立一个空数组
        ArrayList<ProductShopTypeVO> list = new ArrayList<>();
        //循环所有
        for (ProductShopTypeVO productTypeVO : productTypeDTOS) {
            //所有符合条件的
            if(Integer.valueOf(productTypeVO.getTypeParentId()).equals(typeLevel)){
                //通过productTypeDTO.getTypeParentId 获取id 等于查询下级的等级
                List<ProductShopTypeVO> chidRen = getChidRen(productTypeDTOS, Integer.valueOf(productTypeVO.getId()));
                //并将查到的数据放入其中
                productTypeVO.setParentPOS(chidRen);
                //放到空数组中
                list.add(productTypeVO);
            }
        }
        return list;
    }
}
